﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using Util;

public partial class CodeGenerator : System.Web.UI.Page
{
    protected string strconn = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Welcome", "<script>alert('Welcome to use peng.cl's code generator for work')</script>");
    }
    protected void btnpruduceall_Click(object sender, EventArgs e)
    {



    }
    /// <summary>
    /// sqlserver，model层
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModel_Click(object sender, EventArgs e)
    {
        strconn = this.txt_ConnectString.Text;

        SqlClass sql = new SqlClass(strconn);

        DataSet ds = new DataSet();

        ds = sql.GetDataSet("select * from sysobjects where xtype='u'");

        DataView dv = ds.Tables[0].DefaultView;

        for (int i = 0; i < dv.Count; i++)
        {
            string name = dv[i]["name"].ToString();

            string id = dv[i]["id"].ToString();

            
            ProModel(name, txtnamespace.Text.Trim());

            Response.Flush();
        }
    }

    string GetType(string xtype)
    {
        strconn = this.txt_ConnectString.Text;

        SqlClass sql = new SqlClass(strconn);
        
        return sql.GetObject("select top 1 name from systypes where xtype='" + xtype + "'").ToString();
    }


    public void ProModel(string tbname, string strNamespace)
    {
        string strnamespace = strNamespace;

        strconn = this.txt_ConnectString.Text;

        if (strnamespace != "")
        {
            strnamespace = "." + strnamespace;
        }  
        SqlClass sql = new SqlClass(strconn);
        SqlDataReader dr = sql.GetDataReader(" select columnName as name,type  ,length as size,columnDesc   from tableview where tableName='" + tbname + "'");
        tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
        string str = "using System;\nusing System.Data;\nusing System.Configuration;\nusing System.Web;\nusing System.Web.Security;\nusing System.Web.UI;\nusing System.Web.UI.WebControls;\nusing System.Web.UI.WebControls.WebParts;\nusing System.Web.UI.HtmlControls;\nusing System.Data.SqlClient;\n\n";
        str = str + "namespace Model" + strnamespace + "\r\n{\r\n[Serializable]\r\npublic class " + tbname + " \r\n{";
        int k = 0;
        // string strClass = "  \n private int _ID; public int ID\r\n{\r\nget{return _ID;}\r\nset{_ID=value;}\r\n} \n";
        string strClass = "";
        bool HasRecord = false;
        while (dr.Read())
        {
            HasRecord = true;
            string name = Convert.ToString(dr["name"]).Trim();
            name = name.ToUpper().Substring(0, 1) + name.Substring(1);
            string type = dr["type"].ToString();// GetType(Convert.ToString(dr["xtype"])).ToLower();
            string size = Convert.ToString(dr["size"]);
            string desc = "\r\n  /// <summary>\r\n" +
                         "   /// " + dr["columnDesc"].ToString() + "\r\n" +
                         "   /// </summary>\r\n";

            if (type == "int")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "tinyint")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "smallint")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "bigint")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "varchar")
            {

                //strClass = strClass +desc;strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "nvarchar")
            {

                //strClass = strClass +desc;strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "nchar")
            {

                //strClass = strClass +desc;strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "char")
            {

                //strClass = strClass +desc;strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "text")
            {

                //strClass = strClass +desc;strClass = strClass + " public string " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "ntext")
            {

                //strClass = strClass +desc;strClass = strClass + " public string " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "datetime")
            {

                // strClass = strClass +desc;strClass = strClass + " public DateTime " + name + ";\n";

                strClass = strClass + " private DateTime _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public DateTime " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "float")
            {

                // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "decimal")
            {

                // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "numeric")
            {

                // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "image")
            {
                strClass = strClass + " private Image _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public Image " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "bit")
            {
                strClass = strClass + " private bool _" + name + ";\r\n";
                strClass = strClass + desc; 
                strClass = strClass + " public bool " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else
            {
                Response.Write("<br>Error:" + type + "--- 表" + tbname);

            }
            strClass = strClass + "\r\n";
            //SqlDbType.Float 

            k++;
        }
        dr.Close();
        //输出参数

        if (!HasRecord)
        {
            Response.Write("<br>Error:该表没有任何字段- -表" + tbname);

        }
        string strdir = "F:\\code\\model\\" + strnamespace.Replace(".", "") + "\\";
        if (!Directory.Exists(strdir))
        {
            Directory.CreateDirectory(strdir);
        }
        str = str + "\r\n" + strClass + "\n" + "\n}\r\n}\r\n";
        SaveStringToFile(str, strdir + tbname + ".cs");
        //  SaveStringToFile(str, Server.MapPath("app_code/" + tbname + ".txt"));
        // Response.Write(tbname + "<br>");
    }



    public void ProModel(string id, string tbname, string strNamespace)
    {
        string strnamespace = strNamespace;

        strconn = this.txt_ConnectString.Text;

        if (strnamespace != "")
        {
            strnamespace = "." + strnamespace;
        }

        SqlClass sql = new SqlClass(strconn);
        SqlDataReader dr = sql.GetDataReader("select name,xtype,length as size from syscolumns where id=" + id);
        tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
        string str = "using System;\nusing System.Data;\nusing System.Configuration;\nusing System.Web;\nusing System.Web.Security;\nusing System.Web.UI;\nusing System.Web.UI.WebControls;\nusing System.Web.UI.WebControls.WebParts;\nusing System.Web.UI.HtmlControls;\nusing System.Data.SqlClient;\n\n";
        str = str + "namespace Model" + strnamespace + "\r\n{\r\n[Serializable]\r\npublic class " + tbname + " \r\n{";
        int k = 0;
        // string strClass = "  \n private int _ID; public int ID\r\n{\r\nget{return _ID;}\r\nset{_ID=value;}\r\n} \n";
        string strClass = "";
        bool HasRecord = false;
        while (dr.Read())
        {
            HasRecord = true;
            string name = Convert.ToString(dr["name"]).Trim();
            name = name.ToUpper().Substring(0, 1) + name.Substring(1);
            string type = GetType(Convert.ToString(dr["xtype"])).ToLower();
            string size = Convert.ToString(dr["size"]);
            if (type == "int")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else  if (type == "tinyint")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else  if (type == "smallint")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "bigint")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "varchar")
            {

                //strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "nvarchar")
            {

                //strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "nchar")
            {

                //strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "char")
            {

                //strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "text")
            {

                //strClass = strClass + " public string " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "ntext")
            {

                //strClass = strClass + " public string " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "datetime")
            {

                // strClass = strClass + " public DateTime " + name + ";\n";

                strClass = strClass + " private DateTime _" + name + ";\r\n";
                strClass = strClass + " public DateTime " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "float")
            {

                // strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "decimal")
            {

                // strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "numeric")
            {

                // strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "image")
            {

                // strClass = strClass + " public double " + name + ";\n";
                 
                //strClass = strClass + " pivate object _" + name + ";\r\n";
                //strClass = strClass + " public object " + name + "\r\n";
                //strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                //strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
                
                strClass = strClass + " private Image _" + name + ";\r\n";
                strClass = strClass + " public Image " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "bit")
            {
                
                strClass = strClass + " private bool _" + name + ";\r\n";
                strClass = strClass + " public bool " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else
            {
                Response.Write("<br>Error:" + type + "---" + id + "表" + tbname);
                 
            }
            strClass = strClass + "\r\n";
            //SqlDbType.Float 

            k++;
        }
        dr.Close();
        //输出参数

        if (!HasRecord)
        {
            Response.Write("<br>Error:该表没有任何字段-" + id + "-表" + tbname);
          
        }
        string strdir = "F:\\code\\model\\" + strnamespace.Replace(".", "") + "\\";
        if (!Directory.Exists(strdir))
        {
            Directory.CreateDirectory(strdir);
        }
        str = str + "\r\n" + strClass + "\n" + "\n}\r\n}\r\n";
        SaveStringToFile(str, strdir + tbname + ".cs");
        //  SaveStringToFile(str, Server.MapPath("app_code/" + tbname + ".txt"));
       // Response.Write(tbname + "<br>");
    }

    void SaveStringToFile(string str, string filepath)
    {
        StreamWriter stw = new StreamWriter(filepath, false, System.Text.Encoding.Default);
        stw.Write(str);
        stw.Close();
    }

    /// <summary>
    /// sqlserver，dbaccess层
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDBAccess_Click(object sender, EventArgs e)
    {
        strconn = this.txt_ConnectString.Text;

        SqlClass sql = new SqlClass(strconn);

        DataSet ds = new DataSet();

        ds = sql.GetDataSet("select * from sysobjects where xtype='u'");

        DataView dv = ds.Tables[0].DefaultView;

        for (int i = 0; i < dv.Count; i++)
        {
            string name = dv[i]["name"].ToString();

            string id = dv[i]["id"].ToString();

            ProDBAccess(id, name, txtnamespace.Text.Trim());

            Response.Flush();
        }

    }
    void ProDBAccess(string id, string tbname, string strNamespace)
    {
        //操作类
        string strnamespace = strNamespace;

        if (strnamespace != "")
        {
            strnamespace = "." + strnamespace;
        }
        strconn = this.txt_ConnectString.Text;

        SqlClass sql = new SqlClass(strconn);

        SqlDataReader dr = sql.GetDataReader("select name,xtype,length as size from syscolumns where id=" + id);
        tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
        string str = "using System;\nusing System.Data;\nusing System.Configuration;\nusing System.Web;\nusing System.Web.Security;\nusing System.Web.UI;\nusing System.Web.UI.WebControls;\nusing System.Web.UI.WebControls.WebParts;\nusing System.Web.UI.HtmlControls;\nusing System.Data.SqlClient;\r\nusing Model" + strnamespace + ";\r\nusing DBAccess;\r\nusing Util;\r\n";
        str = str + " namespace DBAccess" + strnamespace + " \r\n{ [Serializable]\r\npublic class " + tbname + "D\n{";

        string markadd = " \n/// <summary>\n" +
                       " /// 添加表" + tbname + "记录并返回ID\n" +
                       " /// </summary>\n" +
                       " /// <param name=\"tb\">表" + tbname + "对象</param>\n" +
                       " /// <returns>返回ID</returns>\n";
        string markupdate = " \n /// <summary>\n" +
                             " /// 修改表" + tbname + "记录并反回影响行数\n" +
                             " /// </summary>\n" +
                             " /// <param name=\"tb\">表" + tbname + "对象</param>\n" +
                             " /// <returns>返回影响行数</returns>\n";
        string markdel = "  \n/// <summary>\n" +
                         " /// 通过ID删除记录\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"id\">ID号</param>\n" +
                         " /// <returns>返回影响行数</returns>\n";

        string markdelWhere = "  \n/// <summary>\n" +
                        " /// 按条件删除记录\n" +
                        " /// </summary>\n" +
                        " /// <param name=\"strWhere\">删除的条件如：name='Jim'</param>\n" +
                        " /// <returns>返回影响行数</returns>\n";

        string markgettb = "  \n/// <summary>\n" +
                         " /// 通过ID返回对象" + tbname + "\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"id\">ID号</param>\n" +
                         " /// <returns>返回" + tbname + "</returns>\n";
        string markgetdatareader = "  \n/// <summary>\n" +
                                   " /// 分页返回" + tbname + " datareader\n" +
                                   " /// </summary>\n" +
                                   " ///  <param name=\"pagesize\">返回记录数量</param>\n" +
                                   " ///  <param name=\"index\">当前第几页</param>\n" +
                                   " /// <returns>返回" + tbname + " 记录集</returns>\n";

        string add = markadd + "public  static Result  Add(SqlTransaction sqlTrans,Model" + strnamespace + "." + tbname + " tb)\n{\r\n ";
        string strSql = "string strSql=\" insert into [" + tbname + "](";
        string addField = "";
        string addPara = "";

        string updateNameID = "";

        string update = markupdate + "public  static Result Update (SqlTransaction sqlTrans ,Model" + strnamespace + "." + tbname + " tb)\n{\n  \n";
        string strUpdate = "string strSql =\"update [" + tbname + "] set ";
        string updateField = "";

        string gettb = markgettb + "public  static Model" + strnamespace + "." + tbname + "  GetModel( SqlTransaction  sqlTrans ,int id)\n{\n  Model" + strnamespace + "." + tbname + "  tb=null; \r\n";
        gettb = gettb + "  try{  DataSet ds = new DataSet();\r\n ds =  SqlHelper.ExecuteDataset(sqlTrans, CommandType.Text, \"select * from [" + tbname + "] where id=\"+id.ToString()); \n";
        gettb = gettb + " DataView dv = ds.Tables[0].DefaultView;\r\n                if (dv.Count > 0)      \r\n         {\r\n";
        gettb = gettb + " tb=new Model" + strnamespace + "." + tbname + "(); \n";

        string getModel = markgettb + "public  static Model" + strnamespace + "." + tbname + "  GetModel(SqlConnection sqlconn,int id)\n{\n  Model" + strnamespace + "." + tbname + "  tb=null;SqlDataReader dr=null ;  \r\n";
        getModel = getModel + "  try{ dr =  SqlHelper.ExecuteReader(sqlconn, CommandType.Text, \"select * from [" + tbname + "] where id=\"+id.ToString()); \n";
        getModel = getModel + " if(dr.Read())\n{ tb=new Model" + strnamespace + "." + tbname + "(); \n";

        string getModelsSqlconn = "  \n/// <summary>\n" +
                         " /// 通过条件返回对象" + tbname + "列表\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"strWhere\">条件如name='aaa',小心注入攻击</param>\n" +
                         " /// <returns>返回" + tbname + "</returns>\r\n" +
                         "public static Model" + strnamespace + "." + tbname + "[] GetModels(SqlConnection sqlconn,string strWhere){\r\n" +
                         "try\r\n{\r\n" +
                          "string strSql = \"\"; if (strWhere == \"\") { strWhere = \"  1=1\"; }\r\n " +
                             " strSql = \"select * from [" + tbname + "] Where \" + strWhere;\r\n " +
                             "DataSet ds = SqlHelper.ExecuteDataset(sqlconn, CommandType.Text, strSql);    \r\n        DataView dv =ds.Tables[0].DefaultView;\r\n" +

                             " int total = dv.Count;\r\n Model" + strnamespace + "." + tbname + "[] tbs = new Model" + strnamespace + "." + tbname + "[total];\r\n" +
                             " for (int i = 0; i < dv.Count; i++) {  tbs[i] = new Model" + strnamespace + "." + tbname + "();";



        string getModelsSqlTrans = "  \n/// <summary>\n" +
                     " /// 通过条件返回对象" + tbname + "列表\n" +
                     " /// </summary>\n" +
                     " /// <param name=\"strWhere\">条件如name='aaa',小心注入攻击</param>\n" +
                     " /// <returns>返回" + tbname + "</returns>\r\n" +
                     "public static Model" + strnamespace + "." + tbname + "[] GetModels(SqlTransaction  sqlTrans ,string strWhere){\r\n" +
                     "try\r\n{\r\n" +
                      "string strSql = \"\"; if (strWhere == \"\") { strWhere = \"  1=1\"; }\r\n " +
                         " strSql = \"select * from [" + tbname + "] Where \" + strWhere;\r\n " +
                         "DataSet ds = SqlHelper.ExecuteDataset(sqlTrans, CommandType.Text, strSql);    \r\n        DataView dv =ds.Tables[0].DefaultView;\r\n" +

                         " int total = dv.Count;\r\n Model" + strnamespace + "." + tbname + "[] tbs = new Model" + strnamespace + "." + tbname + "[total];\r\n" +
                         " for (int i = 0; i < dv.Count; i++) {  tbs[i] = new Model" + strnamespace + "." + tbname + "();";

        string getModels = "";
   


        string para = "   SqlParameter[] sqlpar ={ \n ";
        string vals = "";
        int k = 0;
        //string strClass = "public class TB" + tbname + "\n{   \n public int ID;  \n";
        string strClass = "  \n  ";
        bool HasRecord = false;
        while (dr.Read())
        {
            HasRecord = true;
            string name = Convert.ToString(dr["name"]).Trim();
            name = name.ToUpper().Substring(0, 1) + name.Substring(1);
            string type = GetType(Convert.ToString(dr["xtype"]).Trim()).ToLower();
            string size = Convert.ToString(dr["size"]);

            if (name.ToLower() != "id")
            {
                addField += "[" + name + "],";
                addPara += "@" + name + ",";
                updateField += name + "=@" + name + ",";
            }

            if (name.ToLower() == "id" && type == "int")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "int")
            {
                getModel = getModel +"    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Int),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type.ToLower() == "smallint")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt16(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.SmallInt),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt16(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt16(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type == "bit")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToBoolean(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Bit),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToBoolean(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToBoolean(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "bigint")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.BigInt),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "tinyint")
            {
                getModel = getModel +  "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.TinyInt),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "varchar")
            {
                getModel = getModel +  "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.VarChar," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }

            else if (type == "char")
            { getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Char," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "nchar")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.NChar," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "nvarchar")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.NVarChar," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }

           
            else if (type == "text")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Text),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "ntext")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.NText),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "float")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Float),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "decimal")
            {
                getModel = getModel +"    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Decimal),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                 getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "numeric")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Decimal),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "datetime")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.DateTime),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dv[0][\"" + name + "\"]);} \n";

                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDateTime(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type.ToLower() == "smalldatetime")
            {
                getModel = getModel +  "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dr[\"" + name + "\"]);} \n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.SmallDateTime),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDateTime(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type == "image")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =(Image ) dr[\"" + name + "\"];} \n";
                //   para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Binary),\n";
                para = para + "                           new SqlParameter(\"@" + name + "\",SqlDbType.Image),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " = (Image ) dv[0][\"" + name + "\"] ;} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " =  (Image ) dv[i][\"" + name + "\"] ;}\r\n";

            }
            else
            {
                Response.Write("<br>Error:" + id + "-" + tbname + "-" + type);

            }
            //SqlDbType.Float 
            if (name.ToLower() != "id")
            {
                vals = vals + "sqlpar[" + k.ToString() + "].Value=Util.PublicClass.GetNullValue(tb." + name + ");\n"; 
                k++;
            }
            else
            {
                updateNameID = name;
            }           
        }
        dr.Close();
        //输出参数

      

        if (!HasRecord)
        {
            CommonClass.Alert("该表没有任何字段");
          
        }
        /*添加数据方法定义*/
        addField = addField.Substring(0, addField.Length - 1);
        addPara = addPara.Substring(0, addPara.Length - 1);

        strSql = strSql + addField + ")  \"\r\n+\" values(" + addPara + ")\"+\r\n\"; select SCOPE_IDENTITY() \";\r\n";

        add = add + strSql +
            "\r\n Result rs = new Result();\r\n" +
            "try\r\n{\r\n" + para.Substring(0, para.Length - 2) + " \r\n   };\n" +
            vals + "\n " +
            "rs.DataResult = SqlHelper.ExecuteDataset(sqlTrans, CommandType.Text, strSql, sqlpar);\r\n" +
            "if (rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0 && !Convert.IsDBNull(rs.DataResult.Tables[0].Rows[0][0]))\r\n { \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
                "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
                "\r\nfinally{}" +
                "\r\nreturn rs;\r\n}\r\n";

        /*修改数据方法定义区*/
        updateField = updateField.Substring(0, updateField.Length - 1);
        strUpdate = strUpdate + updateField + " where id=@id\";";
        update = update + strUpdate + "\r\n   Result rs = new Result();\r\n try\r\n{\r\n" +
            para + "   new SqlParameter(\"@ID\",SqlDbType.Int)   };\n" +
            vals + " \r\nsqlpar[" + k.ToString() + "].Value=tb." + updateNameID + ";\r\n" +
           " int iflag = SqlHelper.ExecuteNonQuery(sqlTrans, CommandType.Text, strSql, sqlpar);\r\n " +
            "if (iflag>0) { rs.IsSuccess = true; } else { rs.IsSuccess = false; } }\r\n" +
            " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
            " finally { }\r\n" +
            " return rs;\r\n}\r\n";


        /*按ID删除*/
        string delete = markdel + "public static Result  Delete (SqlTransaction sqlTrans,int id)\n{ \n " +
            "string strSql=\"delete from [" + tbname + "] where id=\"+id.ToString();\r\n" +
            " Result rs = new Result();\r\n try\r\n{\r\n" +
            " int iflag = SqlHelper.ExecuteNonQuery(sqlTrans, CommandType.Text, strSql, null);\r\n " +
            "  rs.IsSuccess = true;   }\r\n" +
            " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
            " finally { }\r\n" +
            " return rs;\r\n}\r\n";

        /*按条件删除*/
        markdelWhere = markdelWhere + "\r\npublic static Result  Delete (SqlTransaction sqlTrans,string strWhere)\n{ \n " +
            "string strSql=\"\";\r\n" +
             " Result rs = new Result();\r\n try\r\n{\r\n" +
            "if (strWhere.Trim() != \"\") { strSql = \"delete from ["+tbname +"]  where \" + strWhere; } else { rs.IsSuccess = false; rs.Msg = \"未传入删除条件\"; return rs; }" +
            " int iflag = SqlHelper.ExecuteNonQuery(sqlTrans, CommandType.Text, strSql, null);\r\n " +
            "  rs.IsSuccess = true;   }\r\n" +
            " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
            " finally { }\r\n" +
            " return rs;\r\n}\r\n";


        //按ID查得某条记录
        gettb = gettb + "  \r\n}\r\n}catch (Exception ex){throw ex;}\r\nfinally{ }\r\nreturn tb;}\r\n";
        getModel = getModel + "  \r\n}\r\n}catch (Exception ex){throw ex;}\r\nfinally{dr.Close();dr.Dispose();}\r\nreturn tb;}\r\n";
        //返回model 列表

        getModelsSqlTrans = getModelsSqlTrans + getModels + "}\r\n return tbs;}catch (Exception ex){ throw ex;} }";

        getModelsSqlconn = getModelsSqlconn + getModels + "}\r\n return tbs;}catch (Exception ex){ throw ex;} }"; 



        //分页获取数据
        string strpage = markgetdatareader + "public static  Result  GetDataSet(SqlConnection sqlconn,int pagesize,int index)\n";
        strpage += "{\n";
        strpage += "int nextpage=pagesize*(index-1);\n" +
        " string strSql = \"select top \" + pagesize.ToString() + \" * from   [" + tbname.Replace("'", "").Trim() + "]  where id not in (select top \" + nextpage.ToString() + \" id from  [" + tbname.Replace("'", "").Trim() + "]  order by id desc) order by id desc\";\n" +
       " Result rs = new Result();\r\n try\r\n{\r\n" +
        " rs.DataResult = SqlHelper.ExecuteDataset(sqlconn, CommandType.Text, strSql); \r\n" +
        "if (rs.DataResult.Tables.Count > 0)" +
        "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
        "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
        "\r\nfinally{}" +
        "\r\nreturn rs;\r\n}\r\n";



        //获取全部数据
        string strGetAllData = "public static  Result  GetDataSet(SqlConnection sqlconn)\n";
        strGetAllData += "{\n";
        strGetAllData += " string strSql = \"select   * from   [" + tbname.Replace("'", "").Trim() + "]  order by id  \";\n" +
       " Result rs = new Result();\r\n try\r\n{\r\n" +
        " rs.DataResult = SqlHelper.ExecuteDataset(sqlconn, CommandType.Text, strSql); \r\n" +
        "if (rs.DataResult.Tables.Count > 0)" +
        "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
        "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
        "\r\nfinally{ }" +
        "\r\nreturn rs;\r\n}\r\n";

        //统计共有多少条数据
        string strCount = "public static Result Count(SqlConnection sqlConn)\n";
        strCount += "{\n" +
        " string strSql = \"select count(*)   from   [" + tbname.Replace("'", "").Trim() + "]\";\n" +
        " Result rs = new Result();\r\n try\r\n{\r\n" +
        " rs.DataResult = SqlHelper.ExecuteDataset(sqlConn, CommandType.Text, strSql); \r\n" +
        "if (rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0)" +
        "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
        "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
        "\r\nfinally{ }" +
        "\r\nreturn rs;\r\n}\r\n";

        //string connstr = "\r\n public string connstr = Util.PublicClass.GetConnStr();";
        str = str + "\n" + add + "\n" + update + delete + markdelWhere + gettb + getModel + getModelsSqlconn+getModelsSqlTrans  + strpage + strGetAllData + strCount + "\n}\r\n}";
        string strdir = "F:\\code\\DBAccess\\" + strnamespace.Replace(".", "") + "\\";
        if (!Directory.Exists(strdir))
        {
            Directory.CreateDirectory(strdir);
        }
        SaveStringToFile(str, strdir + tbname + "D.cs");
      

    }
 
    /// <summary>
    /// old，dbaccess
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_OleDbDAccess_Click(object sender, EventArgs e)
    {
        try
        {
            Result rs = Util.CodeGenerate.BuildTablesDBAccess(txtnamespace.Text.Trim());
            if (rs.IsSuccess)
             CommonClass.Alert("生成业务层成功", true);

        }
        catch (Exception ex)
        {
            CommonClass.Alert(ex.Message, true);
        }
        
    }
    public void ProOleModel(string strNamespace,string tbname)
    {
        string strnamespace = strNamespace;

        strconn = this.txt_ConnectString.Text;

        if (strnamespace != "")
        {
            strnamespace = "." + strnamespace;
        }
        DataTable dt = OleDBCommon.OleDBObject.GetColumnList(tbname);
        tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
        string str = "using System;\n\n";
        str = str + "namespace Model" + strnamespace + "\r\n{\r\n[Serializable]\r\npublic class " + tbname + " \r\n{";
        int k = 0;
        // string strClass = "  \n private int _ID; public int ID\r\n{\r\nget{return _ID;}\r\nset{_ID=value;}\r\n} \n";
        string strClass = "";
        bool HasRecord = false;
        for (int i = 0; i < dt.Rows.Count; i++)      
        {        
            HasRecord = true;
            string name = Convert.ToString(dt.Rows[i]["COLUMN_NAME"]).Trim();
            name = name.ToUpper().Substring(0, 1) + name.Substring(1);
            string type = dt.Rows[i]["TypeName"].ToString();// GetType(Convert.ToString(dt.Rows[i]["xtype"])).ToLower();
            string size =dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"].ToString();
            string desc = "\r\n  /// <summary>\r\n" +
                         "   /// "+dt.Rows[i]["DESCRIPTION"].ToString()+"\r\n" +
                         "   /// </summary>\r\n";

            if (type == "int" || type == "short")
            {
                strClass = strClass + " private int _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public int " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "bool")
            {
                strClass = strClass + " private bool _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public bool " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "byte")
            {
                strClass = strClass + " private byte[] _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public byte[] " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }          
            else if (type == "string")
            {

                //strClass = strClass +desc;strClass = strClass + " public string  " + name + ";\n";

                strClass = strClass + " private string _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public string " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }     
           
            else if (type == "datetime")
            {

                // strClass = strClass +desc;strClass = strClass + " public DateTime " + name + ";\n";

                strClass = strClass + " private DateTime _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public DateTime " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "single" || type == "double")
            {

                // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "littlenum")
            {

                // strClass = strClass +desc;strClass = strClass + " public double " + name + ";\n";

                strClass = strClass + " private double _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public double " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }           
            else if (type == "image")
            {
                strClass = strClass + " private Image _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public Image " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else if (type == "bit")
            {
                strClass = strClass + " private bool _" + name + ";\r\n";
                strClass = strClass + desc;
                strClass = strClass + " public bool " + name + "\r\n";
                strClass = strClass + " { get { return _" + name + ";}\r\n";//get
                strClass = strClass + "   set { _" + name + "=value;}\r\n}";//set
            }
            else
            {
                Response.Write("<br>Error:" + type + "--- 表" + tbname);

            }
            strClass = strClass + "\r\n";            //SqlDbType.Float 

       
        }
      
        if (!HasRecord)
        {
            Response.Write("<br>Error:该表没有任何字段- -表" + tbname);

        }
        string strdir = "F:\\code\\model\\" + strnamespace.Replace(".", "") + "\\";
        if (!Directory.Exists(strdir))
        {
            Directory.CreateDirectory(strdir);
        }
        str = str + "\r\n" + strClass + "\n" + "\n}\r\n}\r\n";
        SaveStringToFile(str, strdir + tbname + ".cs");
        //  SaveStringToFile(str, Server.MapPath("app_code/" + tbname + ".txt"));
        // Response.Write(tbname + "<br>");
    }
    void ProOleDBAccess(string id, string tbname, string strNamespace)
    {
        //操作类
        string strnamespace = strNamespace;

        if (strnamespace != "")
        {
            strnamespace = "." + strnamespace;
        }
        strconn = this.txt_ConnectString.Text;

        DataTable dt = OleDBCommon.OleDBObject.GetColumnList(tbname);
        tbname = tbname.ToUpper().Substring(0, 1) + tbname.Substring(1);
        string str = "using System;\nusing System.Data;\nusing System.Data.OleDb;\r\nusing Model" + strnamespace + ";\r\nusing DBAccess;\r\nusing Util;\r\n";
        str = str + " namespace DBAccess" + strnamespace + " \r\n{ [Serializable]\r\npublic class " + tbname + "D\n{";

        string markadd = " \n/// <summary>\n" +
                       " /// 添加表" + tbname + "记录并返回ID\n" +
                       " /// </summary>\n" +
                       " /// <param name=\"tb\">表" + tbname + "对象</param>\n" +
                       " /// <returns>返回ID</returns>\n";
        string markupdate = " \n /// <summary>\n" +
                             " /// 修改表" + tbname + "记录并反回影响行数\n" +
                             " /// </summary>\n" +
                             " /// <param name=\"tb\">表" + tbname + "对象</param>\n" +
                             " /// <returns>返回影响行数</returns>\n";
        string markdel = "  \n/// <summary>\n" +
                         " /// 通过ID删除记录\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"id\">ID号</param>\n" +
                         " /// <returns>返回影响行数</returns>\n";

        string markdelWhere = "  \n/// <summary>\n" +
                        " /// 按条件删除记录\n" +
                        " /// </summary>\n" +
                        " /// <param name=\"strWhere\">删除的条件如：name='Jim'</param>\n" +
                        " /// <returns>返回影响行数</returns>\n";

        string markgettb = "  \n/// <summary>\n" +
                         " /// 通过ID返回对象" + tbname + "\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"id\">ID号</param>\n" +
                         " /// <returns>返回" + tbname + "</returns>\n";
        string markgetdatareader = "  \n/// <summary>\n" +
                                   " /// 分页返回" + tbname + " datareader\n" +
                                   " /// </summary>\n" +
                                   " ///  <param name=\"pagesize\">返回记录数量</param>\n" +
                                   " ///  <param name=\"index\">当前第几页</param>\n" +
                                   " /// <returns>返回" + tbname + " 记录集</returns>\n";

        string add = markadd + "public  static Result  Add(OleDbTransaction OleDbTrans,Model" + strnamespace + "." + tbname + " tb)\n{\r\n ";
        string strSql = "string strSql=\" insert into [" + tbname + "](";
        string addField = "";
        string addPara = "";

        string updateNameID = "";

        string update = markupdate + "public  static Result Update (OleDbTransaction OleDbTrans ,Model" + strnamespace + "." + tbname + " tb)\n{\n  \n";
        string strUpdate = "string strSql =\"update [" + tbname + "] set ";
        string updateField = "";

        string gettb = markgettb + "public  static Model" + strnamespace + "." + tbname + "  GetModel( OleDbTransaction  OleDbTrans ,int id)\n{\n  Model" + strnamespace + "." + tbname + "  tb=null; \r\n";
        gettb = gettb + "  try{  DataSet ds = new DataSet();\r\n ds =  OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, \"select * from [" + tbname + "] where id=\"+id.ToString()); \n";
        gettb = gettb + " DataView dv = ds.Tables[0].DefaultView;\r\n                if (dv.Count > 0)      \r\n         {\r\n";
        gettb = gettb + " tb=new Model" + strnamespace + "." + tbname + "(); \n";

        string getModel = markgettb + "public  static Model" + strnamespace + "." + tbname + "  GetModel(OleDbConnection OleDbconn,int id)\n{\n  Model" + strnamespace + "." + tbname + "  tb=null;OleDbDataReader dr=null ;  \r\n";
        getModel = getModel + "  try{ dr =  OleDbHelper.ExecuteReader(OleDbconn, CommandType.Text, \"select * from [" + tbname + "] where id=\"+id.ToString()); \n";
        getModel = getModel + " if(dr.Read())\n{ tb=new Model" + strnamespace + "." + tbname + "(); \n";

        string getModelsOleDbconn = "  \n/// <summary>\n" +
                         " /// 通过条件返回对象" + tbname + "列表\n" +
                         " /// </summary>\n" +
                         " /// <param name=\"strWhere\">条件如name='aaa',小心注入攻击</param>\n" +
                         " /// <returns>返回" + tbname + "</returns>\r\n" +
                         "public static Model" + strnamespace + "." + tbname + "[] GetModels(OleDbConnection OleDbconn,string strWhere){\r\n" +
                         "try\r\n{\r\n" +
                          "string strSql = \"\"; if (strWhere == \"\") { strWhere = \"  1=1\"; }\r\n " +
                             " strSql = \"select * from [" + tbname + "] Where \" + strWhere;\r\n " +
                             "DataSet ds = OleDbHelper.ExecuteDataset(OleDbconn, CommandType.Text, strSql);    \r\n        DataView dv =ds.Tables[0].DefaultView;\r\n" +

                             " int total = dv.Count;\r\n Model" + strnamespace + "." + tbname + "[] tbs = new Model" + strnamespace + "." + tbname + "[total];\r\n" +
                             " for (int i = 0; i < dv.Count; i++) {  tbs[i] = new Model" + strnamespace + "." + tbname + "();";



        string getModelsOleDbTrans = "  \n/// <summary>\n" +
                     " /// 通过条件返回对象" + tbname + "列表\n" +
                     " /// </summary>\n" +
                     " /// <param name=\"strWhere\">条件如name='aaa',小心注入攻击</param>\n" +
                     " /// <returns>返回" + tbname + "</returns>\r\n" +
                     "public static Model" + strnamespace + "." + tbname + "[] GetModels(OleDbTransaction  OleDbTrans ,string strWhere){\r\n" +
                     "try\r\n{\r\n" +
                      "string strSql = \"\"; if (strWhere == \"\") { strWhere = \"  1=1\"; }\r\n " +
                         " strSql = \"select * from [" + tbname + "] Where \" + strWhere;\r\n " +
                         "DataSet ds = OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, strSql);    \r\n        DataView dv =ds.Tables[0].DefaultView;\r\n" +

                         " int total = dv.Count;\r\n Model" + strnamespace + "." + tbname + "[] tbs = new Model" + strnamespace + "." + tbname + "[total];\r\n" +
                         " for (int i = 0; i < dv.Count; i++) {  tbs[i] = new Model" + strnamespace + "." + tbname + "();";

        string getModels = "";



        string para = "   OleDbParameter[] parameters ={ \n ";
        string vals = "";
        int k = 0;
        //string strClass = "public class TB" + tbname + "\n{   \n public int ID;  \n";
        string strClass = "  \n  ";
        bool HasRecord = false;
        for (int i = 0; i < dt.Rows.Count; i++)      
        {
            HasRecord = true;
            string name = Convert.ToString(dt.Rows[i]["COLUMN_NAME"]).Trim();
            name = name.ToUpper().Substring(0, 1) + name.Substring(1);
            string type = Convert.ToString(dt.Rows[i]["typename"]).Trim().ToLower();
            string size = Convert.ToString(dt.Rows[i]["CHARACTER_MAXIMUM_LENGTH"]);

            if (name.ToLower() != "id")
            {
                addField += "[" + name + "],";
                addPara += "@" + name + ",";
                updateField += name + "=@" + name + ",";
            }

            if (name.ToLower() == "id" && type == "int")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "int")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Integer),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt32(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt32(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type.ToLower() == "short")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt16(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Integer),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToInt16(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToInt16(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type == "bool")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToBoolean(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Boolean),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToBoolean(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToBoolean(dv[i][\"" + name + "\"]);}\r\n";
            }           
            else if (type == "string")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.VarChar," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }

            else if (type == "char")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Char," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "nchar")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.NChar," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "nvarchar")
            {
                getModel = getModel + "  tb." + name + "=Convert.ToString(dr[\"" + name + "\"]); \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.NVarChar," + size + "),\n";
                gettb = gettb + "  tb." + name + "=Convert.ToString(dv[0][\"" + name + "\"]); \n";
                getModels += "  tbs[i]." + name + " = Convert.ToString(dv[i][\"" + name + "\"]); \r\n";
            }
            else if (type == "float" || type == "double")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Double),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "decimal")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Double),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "numeric")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Decimal),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDouble(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDouble(dv[i][\"" + name + "\"]);}\r\n";
            }
            else if (type == "datetime")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Date),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dv[0][\"" + name + "\"]);} \n";

                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDateTime(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type.ToLower() == "smalldatetime")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dr[\"" + name + "\"]);} \n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.SmallDateTime),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " =Convert.ToDateTime(dv[0][\"" + name + "\"]);} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " = Convert.ToDateTime(dv[i][\"" + name + "\"]);}\r\n";

            }
            else if (type == "image")
            {
                getModel = getModel + "    if (dr[\"" + name + "\"] != DBNull.Value){ tb." + name + " =(Image ) dr[\"" + name + "\"];} \n";
                //   para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Binary),\n";
                para = para + "                           new OleDbParameter(\"@" + name + "\",OleDbType.Image),\n";
                gettb = gettb + "    if (dv[0][\"" + name + "\"] != DBNull.Value){ tb." + name + " = (Image ) dv[0][\"" + name + "\"] ;} \n";
                getModels += "  if (dv[i][\"" + name + "\"] != DBNull.Value){tbs[i]." + name + " =  (Image ) dv[i][\"" + name + "\"] ;}\r\n";

            }
            else
            {
                Response.Write("<br>Error:" + id + "-" + tbname + "-" + type);

            }
            //OleDbType.Float 
            if (name.ToLower() != "id")
            {
                vals = vals + "parameters [" + k.ToString() + "].Value=Util.PublicClass.GetNullValue(tb." + name + ");\n";
                k++;
            }
            else
            {
                updateNameID = name;
            }
        }
   



        if (!HasRecord)
        {
            CommonClass.Alert("该表没有任何字段");

        }
        /*添加数据方法定义*/
        addField = addField.Substring(0, addField.Length - 1);
        addPara = addPara.Substring(0, addPara.Length - 1);

        strSql = strSql + addField + ")  \"\r\n+\" values(" + addPara + ")\"+\r\n\"; select SCOPE_IDENTITY() \";\r\n";

        add = add + strSql +
            "\r\n Result rs = new Result();\r\n" +
            "try\r\n{\r\n" + para.Substring(0, para.Length - 2) + " \r\n   };\n" +
            vals + "\n " +
            "rs.DataResult = OleDbHelper.ExecuteDataset(OleDbTrans, CommandType.Text, strSql, parameters);\r\n" +
            "if (rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0 && !Convert.IsDBNull(rs.DataResult.Tables[0].Rows[0][0]))\r\n { \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
                "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
                "\r\nfinally{}" +
                "\r\nreturn rs;\r\n}\r\n";

        /*修改数据方法定义区*/
        updateField = updateField.Substring(0, updateField.Length - 1);
        strUpdate = strUpdate + updateField + " where id=@id\";";
        update = update + strUpdate + "\r\n   Result rs = new Result();\r\n try\r\n{\r\n" +
            para + "   new OleDbParameter(\"@ID\",OleDbType.Integer)   };\n" +
            vals + " \r\nparameters[" + k.ToString() + "].Value=tb." + updateNameID + ";\r\n" +
           " int iflag = OleDbHelper.ExecuteNonQuery(OleDbTrans, CommandType.Text, strSql, parameters);\r\n " +
            "if (iflag>0) { rs.IsSuccess = true; } else { rs.IsSuccess = false; } }\r\n" +
            " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
            " finally { }\r\n" +
            " return rs;\r\n}\r\n";


        /*按ID删除*/
        string delete = markdel + "public static Result  Delete (OleDbTransaction OleDbTrans,int id)\n{ \n " +
            "string strSql=\"delete from [" + tbname + "] where id=\"+id.ToString();\r\n" +
            " Result rs = new Result();\r\n try\r\n{\r\n" +
            " int iflag = OleDbHelper.ExecuteNonQuery(OleDbTrans, CommandType.Text, strSql, null);\r\n " +
            "  rs.IsSuccess = true;   }\r\n" +
            " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
            " finally { }\r\n" +
            " return rs;\r\n}\r\n";

        /*按条件删除*/
        markdelWhere = markdelWhere + "\r\npublic static Result  Delete (OleDbTransaction OleDbTrans,string strWhere)\n{ \n " +
            "string strSql=\"\";\r\n" +
             " Result rs = new Result();\r\n try\r\n{\r\n" +
            "if (strWhere.Trim() != \"\") { strSql = \"delete from [" + tbname + "]  where \" + strWhere; } else { rs.IsSuccess = false; rs.Msg = \"未传入删除条件\"; return rs; }" +
            " int iflag = OleDbHelper.ExecuteNonQuery(OleDbTrans, CommandType.Text, strSql, null);\r\n " +
            "  rs.IsSuccess = true;   }\r\n" +
            " catch (Exception ex) { \r\nrs.IsSuccess = false; \r\nrs.Msg = ex.Message; \r\nthrow ex; \r\n}\r\n" +
            " finally { }\r\n" +
            " return rs;\r\n}\r\n";


        //按ID查得某条记录
        gettb = gettb + "  \r\n}\r\n}catch (Exception ex){throw ex;}\r\nfinally{ }\r\nreturn tb;}\r\n";
        getModel = getModel + "  \r\n}\r\n}catch (Exception ex){throw ex;}\r\nfinally{dr.Close();dr.Dispose();}\r\nreturn tb;}\r\n";
        //返回model 列表

        getModelsOleDbTrans = getModelsOleDbTrans + getModels + "}\r\n return tbs;}catch (Exception ex){ throw ex;} }";

        getModelsOleDbconn = getModelsOleDbconn + getModels + "}\r\n return tbs;}catch (Exception ex){ throw ex;} }";



        //分页获取数据
        string strpage = markgetdatareader + "public static  Result  GetDataSet(OleDbConnection OleDbconn,int pagesize,int index)\n";
        strpage += "{\n";
        strpage += "int nextpage=pagesize*(index-1);\n" +
        " string strSql = \"select top \" + pagesize.ToString() + \" * from   [" + tbname.Replace("'", "").Trim() + "]  where id not in (select top \" + nextpage.ToString() + \" id from  [" + tbname.Replace("'", "").Trim() + "]  order by id desc) order by id desc\";\n" +
       " Result rs = new Result();\r\n try\r\n{\r\n" +
        " rs.DataResult = OleDbHelper.ExecuteDataset(OleDbconn, CommandType.Text, strSql); \r\n" +
        "if (rs.DataResult.Tables.Count > 0)" +
        "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
        "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
        "\r\nfinally{}" +
        "\r\nreturn rs;\r\n}\r\n";



        //获取全部数据
        string strGetAllData = "public static  Result  GetDataSet(OleDbConnection OleDbconn)\n";
        strGetAllData += "{\n";
        strGetAllData += " string strSql = \"select   * from   [" + tbname.Replace("'", "").Trim() + "]  order by id  \";\n" +
       " Result rs = new Result();\r\n try\r\n{\r\n" +
        " rs.DataResult = OleDbHelper.ExecuteDataset(OleDbconn, CommandType.Text, strSql); \r\n" +
        "if (rs.DataResult.Tables.Count > 0)" +
        "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
        "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
        "\r\nfinally{ }" +
        "\r\nreturn rs;\r\n}\r\n";

        //统计共有多少条数据
        string strCount = "public static Result Count(OleDbConnection OleDbConn)\n";
        strCount += "{\n" +
        " string strSql = \"select count(*)   from   [" + tbname.Replace("'", "").Trim() + "]\";\n" +
        " Result rs = new Result();\r\n try\r\n{\r\n" +
        " rs.DataResult = OleDbHelper.ExecuteDataset(OleDbConn, CommandType.Text, strSql); \r\n" +
        "if (rs.DataResult.Tables.Count > 0 && rs.DataResult.Tables[0].Rows.Count > 0)" +
        "{ \r\n rs.IsSuccess = true;\r\n }\r\n else\r\n { \r\n rs.IsSuccess = false;\r\n }\r\n }" +
        "\r\ncatch(Exception ex){\r\n rs.IsSuccess = false ; \r\nrs.Msg = ex.Message; \r\nthrow ex;}" +
        "\r\nfinally{ }" +
        "\r\nreturn rs;\r\n}\r\n";

        //string connstr = "\r\n public string connstr = Util.PublicClass.GetConnStr();";
        str = str + "\n" + add + "\n" + update + delete + markdelWhere + gettb + getModel + getModelsOleDbconn + getModelsOleDbTrans + strpage + strGetAllData + strCount + "\n}\r\n}";
        string strdir = "F:\\code\\DBAccess\\" + strnamespace.Replace(".", "") + "\\";
        if (!Directory.Exists(strdir))
        {
            Directory.CreateDirectory(strdir);
        }
        SaveStringToFile(str, strdir + tbname + "D.cs");


    }

  
   /// <summary>
   /// ole，business
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
    protected void btn_OleDbBLL_Click(object sender, EventArgs e)
    {
        try
        {
           //DataView dv = OleDBCommon.OleDBObject.GetTableList().DefaultView;
           //   for (int i = 0; i < dv.Count; i++)
           //   {
           //       string name = dv[i][2].ToString();
           //       //Util.CodeGenerate.BuildBLL(txtnamespace.Text.Trim(), name);
           //       Util.CodeGenerate.BuildBLL(txtnamespace.Text.Trim(), name);
                  
           //   }
            Result rs= Util.CodeGenerate.BuildTablesBLL(txtnamespace.Text.Trim());
            if(rs.IsSuccess)
                CommonClass.Alert("生成业务层成功", true);
           
        }
        catch (Exception ex)
        {
            CommonClass.Alert(ex.Message, true);
        }
    }

    /// <summary>
    /// ole,model
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btn_OleDbModel_Click(object sender, EventArgs e)
    {
       
        try
        {
          Result rs= Util.CodeGenerate.BuildTablesModel(txtnamespace.Text.Trim());
            if(rs.IsSuccess)
                CommonClass.Alert("生成业务层成功", true);
           
        }
        catch (Exception ex)
        {
            CommonClass.Alert(ex.Message, true);
        }
        
    }

    protected void btn_Insert_Click(object sender, EventArgs e)
    {
        ////Model.zn.UserInfo m_UserInfo = new Model.zn.UserInfo();
        ////m_UserInfo.UserName = "彭小英";
        ////m_UserInfo.Sex = false;
        ////m_UserInfo.Weight = 80.4f;
        ////m_UserInfo.Age = 21;
        ////Result rs = new Result();
        ////try
        ////{
        ////    rs = Bussiness.zn.UserInfoB.Add(m_UserInfo);
        ////    CommonClass.Alert("aa", true);
        ////}
        ////catch (Exception ex)
        ////{
        ////    CommonClass.Alert(ex.Message, true);
        ////}
        //CommonClass.Alert(rs.DataResult.Tables[0].Rows[0][0].ToString(), true);
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        //Model.zn.UserInfo m_UserInfo = new Model.zn.UserInfo();
        //m_UserInfo.UserName = "赵娜";
        //m_UserInfo.Sex = true;
        //m_UserInfo.BirthDay = DateTime.Now;
        //m_UserInfo.Weight = 10.4f;
        //m_UserInfo.Age = 23;
        //m_UserInfo.ID = 6;
        //Result rs = new Result();
        //try
        //{
        //    rs = Bussiness.zn.UserInfoB.Update(m_UserInfo);
        //}
        //catch (Exception ex)
        //{
        //    CommonClass.Alert(ex.Message, true);
        //}      

    }
    protected void btn_GetModel_Click(object sender, EventArgs e)
    {

        //Model.zn.UserInfo m_UserInfo=null;
        //try
        //{
        //    m_UserInfo = Bussiness.zn.UserInfoB.GetModel(6);
        //    CommonClass.Alert(m_UserInfo.UserName, true);
        //}
        //catch (Exception ex)
        //{
        //    CommonClass.Alert(ex.Message, true);
        //}      

    }
    protected void btn_GetModels_Click(object sender, EventArgs e)
    {
    //    Model.zn.UserInfo[] m_UserInfos = null;
    //    try
    //    {
    //        m_UserInfos = Bussiness.zn.UserInfoB.GetModels("UserName='彭承龙'");
    //        foreach (var m_UserInfo in m_UserInfos)
    //        {
    //            CommonClass.Alert(m_UserInfo.Age.ToString()); 
    //        }            
    //    }
    //    catch (Exception ex)
    //    {
    //        CommonClass.Alert(ex.Message, true);
    //    }      

    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        //Result rs = null;
        //try
        //{
        //    rs = Bussiness.zn.UserInfoB.Delete(6);
        //    if (rs.IsSuccess)
        //    { CommonClass.Alert("成功了", true); }
        //    else
        //    { CommonClass.Alert("失败了", true); }
        //}
        //catch (Exception ex)
        //{
        //    CommonClass.Alert(ex.Message, true);
        //}      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CommonClass.Alert("zn");
    }
    /// <summary>
    /// 生成业务逻辑层
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBusiness_Click(object sender, EventArgs e)
    {

    }
}
